#include <iostream>
#include <unistd.h>
#include <cstring>
using namespace std;

int main()
{
    int fds[2] = {0};
    // 创建管道
    pipe(fds);

    // 创建子进程
    int pid = fork();
    if (pid > 0)
    {
        // 父进程
        close(fds[0]);

        char buffer[128];
        // C语言函数需要算\0，不需要-1
        snprintf(buffer, sizeof(buffer), "i am father");
        write(fds[1], buffer, strlen(buffer));

        close(fds[1]);
    }
    else if (pid == 0)
    {
        // 子进程
        close(fds[1]);

        char buffer[128];
        // 系统调用不管\0，所以需要-1
        int n = read(fds[0], buffer, sizeof(buffer) - 1);
        buffer[n] = 0;
        cout << buffer << endl;

        close(fds[0]);
    }
    else
    {
        perror("fork fail\n");
        exit(1);
    }

    return 0;
}